package karaoke.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import karaoke.excepcion.DAOExcepcion;
import karaoke.modelo.Sala;
import karaoke.negocio.GestionSalas;

/**
 * Servlet implementation class ConsultaSalaLocalServlet
 */
@WebServlet("/ConsultaSalaLocalServlet")
public class ConsultaSalaLocalServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ConsultaSalaLocalServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String valor = request.getParameter("idlocal");
		int idlocal = Integer.parseInt((valor.equals("")?"0":valor));
		int idsala = Integer.parseInt((request.getParameter("idsala")==null?"0":request.getParameter("idsala")));
		
		GestionSalas negocio = new GestionSalas();
		Collection<Sala> salas;
		String options = "<option value=''>..: Seleccione Sala :..</option>";
		
		try {
			salas = negocio.buscarPorNombre(idlocal, "");
			if(salas != null){
	        	for(Sala s: salas){
	        		options += "<option value='"+s.getIdSala()+"' "+(idsala==s.getIdSala()?"selected":"")+">"+s.getNombre()+"</option>";
	        	}
	        }
		} catch (DAOExcepcion e) {
			// TODO Auto-generated catch block
			options += "<option value=''>" + e.getMessage() + "</option>";
		}
		
		PrintWriter out = response.getWriter();
		out.println(options);
	}

}
